package com.example.testapplication.ui.home.home_web_fragment;

import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.webkit.WebView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.testapplication.R;
import com.example.testapplication.action.BundleAction;
import com.example.testapplication.action.StatusAction;
import com.example.testapplication.aop.CheckNet;
import com.example.testapplication.aop.Log;
import com.example.testapplication.databinding.FragHomeWebBinding;
import com.example.testapplication.ui.browser.BrowserActivity;
import com.example.testapplication.widget.BrowserView;
import com.example.testapplication.widget.StatusLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import me.goldze.mvvmhabit.base.BaseFragment;
import me.tatarka.bindingcollectionadapter2.BR;

public class BrowserFragment extends BaseFragment<FragHomeWebBinding,HomeWebFragViewModel> implements StatusAction, BundleAction,OnRefreshListener {
    private static final String INTENT_KEY_IN_URL = "url";

    @Log
    public static BrowserFragment newInstance(String url) {
        BrowserFragment fragment = new BrowserFragment();
        Bundle bundle = new Bundle();
        bundle.putString(INTENT_KEY_IN_URL, url);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.frag_home_web;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        binding.wvBrowserView.setBrowserViewClient(new AppBrowserViewClient());
        binding.wvBrowserView.setBrowserChromeClient(new BrowserView.BrowserChromeClient(binding.wvBrowserView));
        binding.wvBrowserView.loadUrl(getString(INTENT_KEY_IN_URL));
        showLoading();
    }

    /**
     * 重新加载当前页
     */
    @CheckNet
    private void reload() {
        binding.wvBrowserView.reload();
    }

    @Override
    public StatusLayout getStatusLayout() {
        return binding.hlBrowserHint;
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        reload();
    }

    @Nullable
    @Override
    public Bundle getBundle() {
        return getArguments();
    }


    private class AppBrowserViewClient extends BrowserView.BrowserViewClient {

        /**
         * 网页加载错误时回调，这个方法会在 onPageFinished 之前调用
         */
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            // 这里为什么要用延迟呢？因为加载出错之后会先调用 onReceivedError 再调用 onPageFinished
            post(() -> showError(listener -> reload()));
        }

        /**
         * 开始加载网页
         */
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {}

        /**
         * 完成加载网页
         */
        @Override
        public void onPageFinished(WebView view, String url) {
            binding.slBrowserRefresh.finishRefresh();
            showComplete();
        }

        /**
         * 跳转到其他链接
         */
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, final String url) {
            String scheme = Uri.parse(url).getScheme();
            if (scheme == null) {
                return true;
            }
            switch (scheme.toLowerCase()) {
                // 如果这是跳链接操作
                case "http":
                case "https":
                    BrowserActivity.start(getActivity(), url);
                    break;
                default:
                    break;
            }
            // 已经处理该链接请求
            return true;
        }
    }
}
